package com.mystudy.management.usermanagement.controller;


import com.alibaba.fastjson.JSONObject;
import com.mystudy.management.usermanagement.laytablemessageconvert.EchartDialogMessageConvert;
import com.mystudy.management.usermanagement.laytablemessageconvert.LayuiSelectMessageConvert;
import com.mystudy.management.usermanagement.service.index2_3Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;


//----------------------------------管理端-访问监管-流量监控----------------------------------
@RestController
@RequestMapping("/admin/index/supervision/float")
public class Index2_3Controller {

    //注入服务
    @Autowired
    private index2_3Service service;

    //注入表格转换器
    @Autowired
    private EchartDialogMessageConvert echartDialogMessageConvert;

    //注入解析器
    @Autowired
    private LayuiSelectMessageConvert selectMessageConvert;

    /*
    返回数据格式：{
        'key1':data1,
        'key2':data2,
        .........

    }
     */
    //----------------------------------------请求量统计------------------------------------------------

    //统计所有请求记录
    @GetMapping("/request/all")
    public JSONObject getAllRequestTrace(){
        JSONObject responseData = echartDialogMessageConvert.lineChartConverter(service.getAllRequestTrace());
        System.out.println(responseData);
        return responseData;
    }

    //近一周的请求记录
    @GetMapping("/request/week")
    public JSONObject getLastWeekRequestTrace(){
        JSONObject responseData = null;
        try {
            responseData = echartDialogMessageConvert.lineChartConverter(service.getLastWeekRequestTrace());
        } catch (ParseException e) {
            e.printStackTrace();
            return new JSONObject();
        }
        System.out.println(responseData);
        return responseData;
    }

    //近一个月的请求记录
    @GetMapping("/request/month")
    public JSONObject getLastMonthRequestTrace(){
        JSONObject responseData = null;
        try {
            responseData = echartDialogMessageConvert.lineChartConverter(service.getLastMonthRequestTrace());
        } catch (ParseException e) {
            e.printStackTrace();
            return new JSONObject();
        }
        System.out.println(responseData);
        return responseData;
    }

    //自定义区间的访问记录
    @PostMapping("/request/daterange")
    public JSONObject getRequestTraceByCustomDateRange(@RequestBody JSONObject requestBody){
        JSONObject responseData = null;
        try {
            Date[] customeDates = selectMessageConvert.index2_3TraceDatesConvert(requestBody);
            responseData = echartDialogMessageConvert.lineChartConverter(service.getRequestTraceByCustomDateRange(customeDates));

        } catch (ParseException e) {
            e.printStackTrace();
            return new JSONObject();
        }
        return  responseData;
    }

    //--------------------------------------授权用户访问量统计------------------------------------------
    //统计所有请求记录
    @GetMapping("/visit/all")
    public JSONObject getAllVisitTrace(){
        JSONObject responseData = echartDialogMessageConvert.lineChartConverter(service.getAllVisitTrace());
        System.out.println(responseData);
        return responseData;
    }

    //近一周的请求记录
    @GetMapping("/visit/week")
    public JSONObject getLastWeekVisitTrace(){
        JSONObject responseData = null;
        try {
            responseData = echartDialogMessageConvert.lineChartConverter(service.getLastWeekVisitTrace());
        } catch (ParseException e) {
            e.printStackTrace();
            return new JSONObject();
        }
        System.out.println(responseData);
        return responseData;
    }
//
    //近一个月的请求记录
    @GetMapping("/visit/month")
    public JSONObject getLastMonthVisitTrace(){
        JSONObject responseData = null;
        try {
            responseData = echartDialogMessageConvert.lineChartConverter(service.getLastMonthVisitTrace());
        } catch (ParseException e) {
            e.printStackTrace();
            return new JSONObject();
        }
        System.out.println(responseData);
        return responseData;
    }
//
    //自定义区间的访问记录
    @PostMapping("/visit/daterange")
    public JSONObject getVisitTraceByCustomDateRange(@RequestBody JSONObject requestBody){
        JSONObject responseData = null;
        try {
            Date[] customeDates = selectMessageConvert.index2_3TraceDatesConvert(requestBody);
            responseData = echartDialogMessageConvert.lineChartConverter(service.getVisitTraceByCustomDateRange(customeDates));

        } catch (ParseException e) {
            e.printStackTrace();
            return new JSONObject();
        }
        return  responseData;
    }

}
